package com.csi.dao.impl;

import com.csi.dao.RoomTypeDao;
import com.csi.domain.RoomType;
import com.csi.utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class RoomTypeDaoImpl extends JDBCUtils implements RoomTypeDao {
    @Override
    public List<RoomType> select() throws SQLException {
        final String sql="select * from hotel_type";
        Connection connection=this.getConnection();
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        List<RoomType> list=new ArrayList<>();
        while (rs.next()){
            RoomType roomType=new RoomType();
            roomType.setTypeId(rs.getInt("typeId"));
            roomType.setTypeName(rs.getString("typeName"));
            roomType.setTypeSize(rs.getString("typeSize"));
            roomType.setIsWifi(rs.getString("isWifi"));
            roomType.setIsWindow(rs.getString("isWindow"));
            roomType.setTypePrice(rs.getDouble("typePrice"));
            roomType.setTypeBed(rs.getString("typeBed"));
            roomType.setTypefac(rs.getString("typeFac"));
            roomType.setTypelayout(rs.getString("typeLayout"));
            roomType.setBeginTime(rs.getTime("beginTime"));
            roomType.setEndTime(rs.getTime("endTime"));
            list.add(roomType);

        }
        release(rs,ps,connection);
        return list;
    }

    @Override
    public int add(RoomType roomType) throws SQLException {
        final String sql="insert into hotel_type(typeName,typeSize,isWifi,isWindow,typePrice,typeBed,typeFac,typeLayout,beginTime,endTime) value(?,?,?,?,?,?,?,?,?,?)";
        Connection connection=this.getConnection();
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,roomType.getTypeName());
        ps.setString(2,roomType.getTypeSize());
        ps.setString(3,roomType.getIsWifi());
        ps.setString(4,roomType.getIsWindow());
        ps.setDouble(5,roomType.getTypePrice());
        ps.setString(6,roomType.getTypeBed());
        ps.setString(7,roomType.getTypefac());
        ps.setString(8,roomType.getTypelayout());
        ps.setTime(9,new Time(roomType.getBeginTime().getTime()));
        ps.setTime(10,new Time(roomType.getEndTime().getTime()));
        int i = ps.executeUpdate();
        release(ps,connection);
        return i;
    }

    @Override
    public int delete(int id) throws SQLException {
        final String sql="delete from hotel_type where typeId=?";
        Connection connection=this.getConnection();
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,id);
        int i = ps.executeUpdate();
        release(ps,connection);
        return i;
    }

    @Override
    public int update(RoomType roomType) throws SQLException {
        final String sql="update hotel_type set typeName=?,typeSize=?,isWifi=?,isWindow=?,typePrice=?,typeBed=?,typeFac=?,typeLayout=?,beginTime=?,endTime=? where(typeId=?)";
        Connection connection=this.getConnection();
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,roomType.getTypeName());
        ps.setString(2,roomType.getTypeSize());
        ps.setString(3,roomType.getIsWifi());
        ps.setString(4,roomType.getIsWindow());
        ps.setDouble(5,roomType.getTypePrice());
        ps.setString(6,roomType.getTypeBed());
        ps.setString(7,roomType.getTypefac());
        ps.setString(8,roomType.getTypelayout());
        ps.setDate(9,new java.sql.Date(roomType.getBeginTime().getTime()));
        ps.setDate(10,new java.sql.Date(roomType.getEndTime().getTime()));
        ps.setInt(11,roomType.getTypeId());
        int i = ps.executeUpdate();
        release(ps,connection);
        return i;
    }
}
